<template>
    <div class="index-lists">
        <el-card class="!border-none" shadow="never">
            <el-form ref="formRef" class="mb-[-16px]" :model="queryParams" :inline="true">
                <el-form-item label="" prop="customerName">
                    <el-input class="w-[280px]" v-model="queryParams.customerName" placeholder="请输入姓名"/>
                </el-form-item>
                <el-form-item label="" prop="caseType">
                    <el-select v-model="queryParams.caseType" class="w-[280px]" clearable placeholder="案件类型">
<!--                        <el-option label="全部" value="" />-->
                        <el-option v-for="(item, index) in dictData.case_type" :key="index" :label="item.name" :value="item.value"/>
                    </el-select>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="resetPage">查询</el-button>
                    <el-button @click="resetParams">重置</el-button>
                    <el-button v-perms="['customer:add']" type="primary" @click="handleAdd()">
                        <template #icon>
                            <icon name="el-icon-Plus" />
                        </template>
                        新增
                    </el-button>
                </el-form-item>
            </el-form>
        </el-card>
        <el-card class="!border-none mt-4" shadow="never">
<!--            <div><el-button v-perms="['customer:add']" type="primary" @click="handleAdd()"><template #icon><icon name="el-icon-Plus" /></template>新增</el-button></div>-->
            <el-table
                class="mt-4"
                size="large"
                v-loading="pager.loading"
                :data="pager.lists"
            >
                <el-table-column label="姓名" prop="customerName" min-width="80" />
                <el-table-column label="电话" prop="customerCall" min-width="100" />
                <el-table-column label="身份证号" prop="idNumber" min-width="100" />
                <el-table-column label="性别" prop="sex" min-width="50">
                    <template #default="{ row }">
                        <dict-value :options="dictData.sex" :value="row.sex" />
                    </template>
                </el-table-column>
                <el-table-column label="住址" prop="address" min-width="100" />
                <el-table-column label="银行" prop="bank" min-width="100" />
                <el-table-column label="欠款金额" prop="amountDue" min-width="100" />
                <el-table-column label="案件类型" prop="caseType" min-width="100">
                    <template #default="{ row }">
                        <dict-value :options="dictData.case_type" :value="row.caseType" />
                    </template>
                </el-table-column>
<!--                <el-table-column label="备注" prop="remark" min-width="100" />-->
<!--                <el-table-column label="状态：0不可用/1可用" prop="status" min-width="100" />-->
                <el-table-column label="是否分配到案件" prop="state" min-width="100" >
                    <template #default="{ row }">
                        <dict-value :options="dictData.allot_state" :value="row.state" />
                    </template>
                </el-table-column>
<!--                <el-table-column label="调解员id" prop="courtClerkId" min-width="100" />-->
<!--                <el-table-column label="是否自动分配 1是 0否" prop="isAutoAllot" min-width="100" />-->
<!--                <el-table-column label="调解状态 0待调解，1调解成功，2调解失败，3已过期，4二次调解中 5二次调解失败 6案件处理中" prop="mediationState" min-width="100" />-->
                <el-table-column label="操作" width="120" fixed="right">
                    <template #default="{ row }">
                        <el-button v-show="row.state===0" v-perms="['customer:edit']" type="primary" link @click="handleEdit(row)">
                            编辑
                        </el-button>
                        <el-button v-perms="['customer:del']" type="danger" link @click="handleDelete(row.customerNumber)">
                            删除
                        </el-button>
                    </template>
                </el-table-column>
            </el-table>
            <div class="flex justify-end mt-4">
                <pagination v-model="pager" @change="getLists" />
            </div>
        </el-card>
        <edit-popup
            v-if="showEdit"
            ref="editRef"
            :dict-data="dictData"
            @success="getLists"
            @close="showEdit = false"
        />
    </div>
</template>
<script lang="ts" setup name="customer">
import { customerDelete, customerLists } from '@/api/customer'
import { useDictData } from '@/hooks/useDictOptions'
import { usePaging } from '@/hooks/usePaging'
import feedback from '@/utils/feedback'
import EditPopup from './edit.vue'
const editRef = shallowRef<InstanceType<typeof EditPopup>>()
const showEdit = ref(false)
const queryParams = reactive({
    customerName: '',
    caseType: '',
})

const { pager, getLists, resetPage, resetParams } = usePaging({
    fetchFun: customerLists,
    params: queryParams
})

const { dictData } = useDictData<{
    sex: any[]
    case_type: any[]
    allot_state: any[]
}>(['sex','case_type','allot_state'])


const handleAdd = async () => {
    showEdit.value = true
    await nextTick()
    editRef.value?.open('add')
}

const handleEdit = async (data: any) => {
    showEdit.value = true
    await nextTick()
    editRef.value?.open('edit')
    editRef.value?.getDetail(data)
}

const handleDelete = async (customerNumber: number) => {
    await feedback.confirm('确定要删除？')
    await customerDelete({ customerNumber })
    feedback.msgSuccess('删除成功')
    getLists()
}

getLists()
</script>
